<template>
	<view class="w-full h-[100vh] flex flex-col items-center gap-[20rpx]">
		<text class="text-[32rpx] px-[30rpx] box-border font-bold text-[red] w-full text-center leading-[1.5em] mt-[50rpx] mb-[100rpx]">{{tips}}</text>
		<text class="text-[28rpx] leading-[2em] mb-[20rpx]">正式服服务地址：{{onlineBaseUrl}}</text>
		<text class="text-[28rpx] leading-[2em] mb-[20rpx]">测试服服务地址：{{testBaseUrl}}</text>
		<text class="text-[28rpx] leading-[2em] mb-[20rpx]">当前服务地址：{{baseUrl}}</text>
		<text class="text-[28rpx] leading-[2em] mb-[20rpx]">当前所在的服务为：【{{nowServerStatus === 'test' ? '测试服' : '正式服'}}】</text>
		<text class="text-[28rpx] leading-[2em] mb-[20rpx]">当前代码更新标志：{{updateTimeStamp}}</text>
		<up-button class="mb-[20rpx]" @click="toggleServer('online')" v-if="nowServerStatus === 'test'" text="切换到正式服"></up-button>
		<up-button @click="toggleServer('test')" text="切换到测式服" v-if="nowServerStatus === 'online'"></up-button>
		<view class="flex flex-row justify-around">
			<up-button @click="handlerStorage('look')" text="查看本地缓存"></up-button>
			<up-button @click="handlerStorage('clear')" text="清除本地缓存"></up-button>
		</view>
		<up-picker :show="showStorageList" :columns="storageList" @confirm="storageConfirm" @cancel="showStorageList = false"></up-picker>
		<up-modal :show="showModel" :title="title" :content="content" @confirm="showModel = false"></up-modal>
	</view>
</template>

<script setup>
	import { ref, computed } from 'vue';
	import { ZHS_SERVER_TYPE_KEY, BASE_URL, TEST_BASE_URL, ONLINE_BASE_URL, UPDATE_TIME_STAMP } from '@/config/http.js';
	const wellKnownSaying = [
		'谁对时间越吝啬，时间就对谁越慷慨。—李大钊',
		'没有绝望的处境，只有对处境绝望的人。—哈尔西',
		'辛勤的蜜蜂永远没有时间悲哀。—布莱克',
		'要成就一番大事业，必须从小事做起。—列宁',
		'业精于勤，荒于戏。行成于思，毁于随。—韩愈',
		'书山有路勤为径，学海无涯苦作舟。—佚名',
		'天才是百分之一的灵感加百分之九十九的汗水。—爱迪生',
		'不积跬步，无以成千里，不积小流，无以成江海—《荀子•劝学》',
		'财富不应当是生命的目的，它只是生活的工具。—比才',
		'节约与勤勉是人类的两个名医。—卢梭',
		'决心就是力量，信心就是成功。—列夫·托尔斯泰',
		'一个人越知道时间的价值，越倍觉失时的痛苦呀! —但丁',
		'完成工作的方法是爱惜每一分钟。—达尔文',
		'过于求速是做事的最大危险之一。—培根',
		'劳动是一切知识的源泉。—陶铸',
		'穷则独善其身，达则兼济天下—《孟子•尽心章句上》',
		'与朋友交，只取其长，不计其短。—（唐）李煜',
		'土扶可成墙，积德为厚地。—李白',
		'不满足是向上的车轮。—鲁迅',
		'你想成为幸福的人吗？但愿你首先学会吃得起苦。—屠格涅夫',
		'想不付出任何代价而得到幸福，那是神话。—徐特立',
		'谨慎比大胆要有力得多。—雨果',
		'盛满易为灾，谦冲恒受福。—张廷玉',
		];

	const baseUrl = ref(BASE_URL);
	const testBaseUrl = ref(TEST_BASE_URL);
	const onlineBaseUrl = ref(ONLINE_BASE_URL);
	const updateTimeStamp = ref(UPDATE_TIME_STAMP);

	const nowServerStatus = computed(() => {
		if (BASE_URL === TEST_BASE_URL) {
			return 'test';
		} else if (BASE_URL === ONLINE_BASE_URL) {
			return 'online';
		}
		return '';
	})

	const tips = computed(() => {
		return wellKnownSaying[Math.floor(Math.random() * wellKnownSaying.length)]
	})

	const toggleServer = (type) => {
		try{
			uni.setStorageSync(ZHS_SERVER_TYPE_KEY, type);
			uni.showToast({
				title: `切换到${type === 'test' ? '测试服': '正式服'}成功,重启小程序生效`,
				icon: 'none'
			})
		}catch(e){}
	}


	const showModel = ref(false);
	const title = ref('温馨提示');
	const content = ref('');

	// 缓存清除查看
	const showStorageList = ref(false);
	const storageList = ref([]);
	let storageAction = 'look';
	const storageInit = (action) => {
		const { keys=[] } = uni.getStorageInfoSync();
		console.log('---- keys----:',  keys);
		storageList.value = action === 'clear' ? [['全部', ...keys]] : [[...keys]];
		showStorageList.value = true;
	}
	const handlerStorage = (action) => {
		storageInit(action);
		storageAction = action;
	}
	// 根据选择的key清除缓存
	const storageConfirm = (e) => {
		console.log('---- storageConfirm----:',  e);
		if (e.value[0] === '全部') {
			uni.clearStorageSync();
		} else {
			if (storageAction === 'look') {
				const content = uni.getStorageSync(e.value[0]);
				uni.showModal({
						content: typeof content === 'object' ? JSON.stringify(content) : content,
						showCancel: false
					})
				return;
			}
			uni.removeStorageSync(e.value[0]);
		}
		uni.showToast({
			icon: 'none',
			title: '缓存清除成功'
		})
	}
</script>

<style>

</style>
